package com.example;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class HelloWorld extends Activity {
    /**
     * Called when the activity is first created.
     */
    Spinner spinner;
    Button submitButton;
    String subject;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AccountManager manager = AccountManager.get(this);
        Account[] accounts = manager.getAccountsByType("com.google");
        if (accounts.length > 0) {
            setContentView(R.layout.main);
            addItemsOnSpinner();
            addListenerOnButton();
        } else {
            AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
            dlgAlert.setMessage("No mail server configured. Please configure it and come again !!!");
            dlgAlert.setTitle("Error !!!");
            dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }
    }

    public void addItemsOnSpinner() {
        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.subject_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    public void addListenerOnButton() {
        spinner = (Spinner) findViewById(R.id.spinner);
        submitButton = (Button) findViewById(R.id.btnSubmit);
        submitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                subject = String.valueOf(spinner.getSelectedItem());
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                String IMEINumber = telephonyManager.getDeviceId();

                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("application/octet-stream");
                String[] toMailList = getResources().getStringArray(R.array.send_to_mail_address);
                i.putExtra(Intent.EXTRA_EMAIL, toMailList);
                i.putExtra(Intent.EXTRA_SUBJECT, IMEINumber + " " + subject);
                i.putExtra(Intent.EXTRA_TEXT, "");
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(HelloWorld.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }

//                Toast.makeText(HelloWorld.this, "Error is - " + String.valueOf(spinner.getSelectedItem()), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
